package com.yami.shop.api.controller.tudu;

import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yami.shop.bean.dto.tudu.PPTDetailsDto;
import com.yami.shop.bean.model.tudu.PPTDetails;
import com.yami.shop.bean.model.tudu.PPTFile;
import com.yami.shop.dao.tudu.PPTDetailsMapper;
import com.yami.shop.dao.tudu.PPTFileMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFHyperlink;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextBox;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/p/pptmain")
@Api(tags = "PPT接口")
public class PPTController{

    @Autowired
    private  PPTDetailsMapper pptDetailsMapper;

    @Autowired
    private  PPTFileMapper pptFileMapper;


    @GetMapping("/pptall")
    @ApiOperation(value = "PPT查询", notes = "PPT查询")
    public Object all(Long ppt_id)  {
        QueryWrapper<PPTDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("ppt_id",ppt_id);
        List<PPTDetails> pptall = pptDetailsMapper.selectList(wrapper);
        int PPTALL  = pptall.size();
        if (PPTALL != 0){
            return pptall;
        }else {
            int size = pptFileMapper.delete(new LambdaQueryWrapper<PPTFile>()
                    .in(PPTFile::getPptId,ppt_id));
            return "PPT无内容已删除";
        }
    }

    @GetMapping("/cjpptins")
    @ApiOperation(value = "PPT创建", notes = "创建ppt")
    public PPTFile pptins(PPTFile pptFile)  {
        pptFileMapper.insert(pptFile);

        QueryWrapper<PPTFile> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",pptFile.getUserId());
        wrapper.orderByAsc("ppt_id");
        List<PPTFile> pptid = pptFileMapper.selectList(wrapper);
        if (pptid == null || pptid.isEmpty()) {
            return null;
        }
        return pptid.get(pptid.size() - 1);
        
    }

    @GetMapping("/pptins")
    @ApiOperation(value = "PPT添加", notes = "PPT单个商品添加")
    public String ins(PPTDetails pptDto)  {
         pptDetailsMapper.insert(pptDto);
        return null;
    }


    @GetMapping("/pptdel")
    @ApiOperation(value = "PPT删除", notes = "PPT单个商品删除")
    public List del(Long details_id) {
        QueryWrapper<PPTDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("details_id", details_id);
        List<PPTDetails> pptid = pptDetailsMapper.selectList(wrapper);
        List Str = Collections.singletonList(pptid.get(0).getPptId());
        pptDetailsMapper.delete(wrapper);
        return Str;
    }



    @PutMapping("/pptup")
    @ApiOperation(value = "PPT显示价格修改", notes = "PPT显示价格修改")
    public PPTDetails pptup(PPTDetails pptDetup){
        LambdaUpdateWrapper<PPTDetails> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PPTDetails::getDetailsId, pptDetup.getDetailsId())
                .set(PPTDetails::getGoodsPrice, pptDetup.getGoodsPrice());
        pptDetailsMapper.updateById(pptDetup);
        return null;
    }


    @PutMapping("/pptnameup")
    @ApiOperation(value = "PPT名称修改", notes = "PPT名称修改")
    public PPTFile nameup(PPTFile pptFile) {
        LambdaUpdateWrapper<PPTFile> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PPTFile::getUserId, pptFile.getUserId())
                .eq(PPTFile::getPptId,pptFile.getPptId())
                .set(PPTFile::getPptName, pptFile.getPptName());
        pptFileMapper.updateById(pptFile);
        return null;
    }

    @GetMapping("/ppt")
    @ApiOperation(value = "PPT导出", notes = "PPT导出")
    public void main(PPTDetailsDto pptDetails) throws Exception {

        QueryWrapper<PPTDetails> wrapper = new QueryWrapper<>();

        wrapper.eq("ppt_id",pptDetails.getPptId());

        List<PPTDetails> pptall =  pptDetailsMapper.selectList(wrapper);

        QueryWrapper<PPTFile> wrap = new QueryWrapper<>();
        wrap.eq("ppt_id",pptDetails.getPptId());
        List<PPTFile> name=  pptFileMapper.selectList(wrap);

        //String name = pptDetails.getPptFile().getPptName();

        XMLSlideShow ppt = new XMLSlideShow();
        for (PPTDetails ppta:pptall) {

            XSLFSlide slide = ppt.createSlide();//创建幻灯片

            XSLFTextBox textBox = slide.createTextBox();
            textBox.setAnchor(new Rectangle2D.Double(200,450, 200, 200));
            textBox.addNewTextParagraph().addNewTextRun().setText("价格:"+String.valueOf(ppta.getGoodsPrice()));//商品价格

            XSLFTextBox textBo = slide.createTextBox();
            textBo.setAnchor(new Rectangle2D.Double(200,350, 200, 200));
            textBo.addNewTextParagraph().addNewTextRun().setText("名称:"+String.valueOf(ppta.getGoodsName()));//商品名称

            XSLFTextBox textB = slide.createTextBox();
            textB.setAnchor(new Rectangle2D.Double(200,400, 200, 200));
            textB.addNewTextParagraph().addNewTextRun().setText("详情:"+String.valueOf(ppta.getGoodsDetails()));//商品详情

            //插入图片
            byte[] bt = FileUtils.readFileToByteArray(new File(ppta.getGoodsPic()));
            int idx = ppt.addPicture(bt, XSLFPictureData.PICTURE_TYPE_PNG);
            XSLFPictureShape pic = slide.createPicture(idx);
            pic.setAnchor(new Rectangle2D.Double(50, 50, 400, 300));

            //创建一个文本链接
            XSLFTextBox linkText = slide.createTextBox();
            linkText.setAnchor(new Rectangle2D.Double(30, 500, 500, 100));
            XSLFTextRun r = linkText.addNewTextParagraph().addNewTextRun();
            r.setText(ppta.getGoodsLink());
            XSLFHyperlink link = r.createHyperlink();
            link.setAddress(ppta.getGoodsLink());

        }
        ppt.write(new FileOutputStream("C:\\Users\\zwli\\Desktop\\"+name.get(0).getPptName()+".pptx"));

    }
}